from App.exts import db
from dataclasses import dataclass


@dataclass
class User(db.Model):
    __tablename__ = 'user'

    id: int = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
    username: str = db.Column(db.String(50), nullable=False, unique=True, comment='用户名')
    nickname: str = db.Column(db.String(50), nullable=False, default='', comment='昵称')
    password: str = db.Column(db.String(255), nullable=False, comment='密码')
    salt: str = db.Column(db.String(32), nullable=False, comment='加密盐')
    email: str = db.Column(db.String(100), unique=True, comment='电子邮箱')
    is_active: bool = db.Column(db.Boolean, default=True, comment='是否激活')
    is_admin: bool = db.Column(db.Boolean, default=False, comment='是否为管理员')
    create_time: db.DateTime = db.Column(db.DateTime, default=db.func.current_timestamp(), comment='创建时间')
    update_time: db.DateTime = db.Column(db.DateTime, default=db.func.current_timestamp(),
                                         onupdate=db.func.current_timestamp(), comment='最后修改时间')

    # 全参数构造方法
    def __init__(self, username: str, nickname: str, password: str, salt: str, email: str, is_active: bool,
                 is_admin: bool, create_time: db.DateTime, update_time: db.DateTime):
        self.username = username
        self.nickname = nickname
        self.password = password
        self.salt = salt
        self.email = email
        self.is_active = is_active
        self.is_admin = is_admin
        self.create_time = create_time
        self.update_time = update_time

    def __eq__(self, other):
        if isinstance(other, User):
            return self.id == other.id
        return False

    def __repr__(self):
        return f'User[id={self.id}, username={self.username}, nickname={self.nickname}, email={self.email}, is_active={self.is_active}, is_admin={self.is_admin}, create_time={self.create_time}, update_time={self.update_time}]'
